package ${packageName}.action;

import com.jspx.txweb.annotation.HttpMethod;
import com.jspx.txweb.annotation.Operate;
import com.jspx.txweb.annotation.Safety;
import com.jspx.txweb.annotation.Operate;
import com.jspx.txweb.IUserSession;
import ${packageName}.table.${className};
import ${packageName}.view.*;

/**
 * Created by jspx.net maker
 * User: chenYuan
 * DateTime: ${date.string('yyyy-MM-dd HH:mm')}
 * ${packageName}.action.${className}
 * 数据管理
 */
@HttpMethod(caption = "${title}管理")
public class ${className}Action extends ${className}ListView {
    public ${className}Action() {

    }

    private Long[] idArray = null;
    @Safety(request = false)
    public void setIdArray(Long[] idArray) {
        this.idArray = idArray;
    }

    /**
     * 删除${className}对象,支持数组方式
     * @throws Exception
     */
    @Operate(caption="删除")
    public void delete() throws Exception {
        setActionResult(ROC);
        //登陆判断
        if (isGuest()) {
            addFieldInfo(Environment.warningInfo, language.getLang(Environment.lan_notLogin));
            return;
        }
        IUserSession userSession = (IUserSession) getUserSession();

        Long[] ids = getLongArray("id", idArray);
        if (ArrayUtil.isEmpty(ids)&&getId()>0) ids = new Long[]{getId()};
        if (ArrayUtil.isEmpty(ids)) {
            addFieldInfo(Environment.warningInfo, language.getLang(Environment.lan_needSelect));
            return;
        }
        //删除日志记录
        StringBuilder actionLog = new StringBuilder();
        for (long id : ids) {
            ${className} ${className.firstLowerCase()} = (${className}) ${className.firstLowerCase()}DAO.get(${className}.class, id);
            if (${className.firstLowerCase()}DAO.delete(${className}.class, id, false) > 0) {
                addActionMessage(language.getLang(Environment.lan_deleteSuccess) + ${className.firstLowerCase()}.getId());
                actionLog.append(BeanUtil.toString( ${className.firstLowerCase()}));
            }
        }
        setActionLogContent(actionLog.toString());
    }

     /**
     * 保存${className}对象
     * @throws Exception
     */
    @Operate(caption="保存")
    public void save() throws Exception {
        //登陆判断
        if (isGuest()) {
            addFieldInfo(Environment.warningInfo, language.getLang(Environment.lan_notLogin));
            return;
        }
        IUserSession userSession = (IUserSession) getUserSession();

        ${className} ${className.firstLowerCase()} = (${className}) getBean(${className}.class);

        //放入用户信息
        ${className.firstLowerCase()}.setPutName(userSession.getName());
        ${className.firstLowerCase()}.setPutUid(userSession.getUid());
        ${className.firstLowerCase()}.setIp(request.getRemoteAddr());

        int x = ${className.firstLowerCase()}DAO.save(${className.firstLowerCase()});
        if (x > 0) {
            setActionLogContent(BeanUtil.toString( ${className.firstLowerCase()}));
            addActionMessage(language.getLang(Environment.lan_saveSuccess) + x);
        } else {
            addFieldInfo(Environment.warningInfo, language.getLang(Environment.lan_namespaceError)+ x);
        }
    }

    /**
     * 编辑${className}对象
     * @throws Exception
     */
    @Operate(caption="编辑")
    public void update() throws Exception {

        //登陆判断
        if (isGuest()) {
            addFieldInfo(Environment.warningInfo, language.getLang(Environment.lan_notLogin));
            return;
        }
        IUserSession userSession = (IUserSession) getUserSession();

        ${className} ${className.firstLowerCase()} = (${className}) getBean(${className}.class);


        ${className.firstLowerCase()}.setPutName(userSession.getName());
        ${className.firstLowerCase()}.setPutUid(userSession.getUid());
        ${className.firstLowerCase()}.setIp(request.getRemoteAddr());

        ${className} ${className.firstLowerCase()}Old = (${className}) ${className.firstLowerCase()}DAO.get(${className}.class, ${className.firstLowerCase()}.getId());
        if (${className.firstLowerCase()}Old == null) {
            addFieldInfo(Environment.warningInfo,language.getLang(Environment.lan_updateFailure));
            return;
        }
        ${className.firstLowerCase()}.setCreateDate(${className.firstLowerCase()}Old.getCreateDate());
        int x = ${className.firstLowerCase()}DAO.update(${className.firstLowerCase()});
        if (x > 0) {
            addActionMessage(language.getLang(Environment.lan_updateSuccess));
            setActionLogContent(BeanUtil.toString( ${className.firstLowerCase()}));
            setActionResult("list");
        } else {
            addFieldInfo(Environment.warningInfo,language.getLang(Environment.lan_updateFailure)+ x);
        }
    }


    @Operate(caption = "提前")
    public void sortDate() throws Exception {
        setActionResult(ROC);
        if (isGuest()) {
            addFieldInfo(Environment.warningInfo, language.getLang(Environment.lan_notLogin));
            return;
        }

        Long[] ids = getLongArray("id", idArray);
        if (ArrayUtil.isEmpty(ids)) {
            addFieldInfo(Environment.warningInfo, language.getLang(Environment.lan_invalidParameter));
            return;
        }

        StringBuilder actionLog = new StringBuilder();
        for (Long id : ids) {
            ${className} ${className.firstLowerCase()} = (${className})${className.firstLowerCase()}DAO.get(${className}.class, id, false);
            if (${className.firstLowerCase()} == null) continue;
            ${className.firstLowerCase()}.setSortDate(new Date());
            int r = ${className.firstLowerCase()}DAO.update(${className.firstLowerCase()}, new String[]{"sortDate"});
            if (r > 0) {
              actionLog.append(id);
                addActionMessage(language.getLang(Environment.lan_saveSuccess));
            } else {
                addFieldInfo(Environment.warningInfo,language.getLang(Environment.lan_updateFailure));
            }
        }
        setActionLogTitle(ArrayUtil.toString(ids, "|"));
        setActionLogContent(actionLog);
    }


    private int sortType = 0;
    public void setSortType(int sortType) {
        this.sortType = sortType;
    }
    /**
    * 排序类型
    */
    @Operate(caption = "排序")
    public void sortType() throws Exception {
        setActionResult(ROC);
        Long[] ids = getLongArray("id", idArray);
        if (ArrayUtil.isEmpty(ids)) {
            addFieldInfo(Environment.warningInfo, language.getLang(Environment.lan_invalidParameter));
            return;
        }
        IUserSession userSession = (IUserSession) getUserSession();
        if (${className.firstLowerCase()}DAO.updateSortType(ids, getInt("sortType",sortType))) {
            addActionMessage(language.getLang(Environment.lan_saveSuccess));
            setActionLogContent(ArrayUtil.toString(ids, ","));
        } else {
            addFieldInfo(Environment.warningInfo,language.getLang(Environment.lan_updateFailure));
        }
    }

    /**
    * 置顶还原
    * @throws Exception
    */
    @Operate(caption = "置顶还原")
    public void clearSortType() throws Exception {
        setActionResult(ROC);
        Long[] ids = getLongArray("id", idArray);
        if (ArrayUtil.isEmpty(ids)) {
            addFieldInfo(Environment.warningInfo, language.getLang(Environment.lan_invalidParameter));
            return;
        }
        if (${className.firstLowerCase()}DAO.updateSortType(ids, 0)) {
            addActionMessage(language.getLang(Environment.lan_saveSuccess));
            setActionLogContent(ArrayUtil.toString(ids, ","));
        } else {
            addFieldInfo(Environment.warningInfo,language.getLang(Environment.lan_updateFailure));
        }
    }

    @Override
    public String execute() throws Exception {
        if (isMethodInvoked()) {
            clearData();
            ${className.firstLowerCase()}DAO.evict(${className}.class);
        }
        return super.execute();
    }
}